/*
2021-7-12
https://www.acwing.com/problem/content/190/
*/

#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
typedef pair<int,int>PII;
const int N=155;
char g[N][N];
int dist[N][N];
PII q[N*N];
int n,m;

int bfs(PII strat)
{
    int dx[]={1,1,-1,-1,2,2,-2,-2},dy[]={2,-2,2,-2,1,-1,1,-1};
    
    int hh=0,tt=0;
    q[0]=strat;
    memset(dist,-1,sizeof dist);
    dist[strat.first][strat.second]=0;
    
    while(hh<=tt)
    {
        auto temp=q[hh++];
        for(int i=0;i<8;i++)
        {
            int a=temp.first+dx[i],b=temp.second+dy[i];
            if(a<0 || a>=n || b<0 || b>=m) continue;
            if(dist[a][b]!=-1) continue;
            if(g[a][b]=='*') continue;
            dist[a][b]=dist[temp.first][temp.second]+1;
            q[++tt]={a,b};
            if(g[a][b]=='H') return dist[a][b];
        }
    }
    return -1;
}

int main()
{
    PII strat;
    cin>>m>>n;
    for(int i=0;i<n;i++)
    {
        cin>>g[i];
        for(int j=0;j<m;j++)
        {
            if(g[i][j]=='K')
            strat={i,j};
        }
    }
    
    cout<<bfs(strat)<<endl;
    
    return 0;
}
